%% meas para
tableList = {'Bat1Table.csv','BatFLITable.csv','HumanTable.csv','Bat2Table.csv'};
% tableList = {'BatFLITable.csv'};
channelWidth = 20; % um
pixSize = 0.34; % um/pix

exportRootDir = 'exportForECalc';

areaPlotRange = [0 500];
eModPlotRange = [0 3];
viscPlotRange = [0 30];
tau2PlotRange = [0 60];

flagPlotRanges = 1;

%% find samples
for idxTableList = 1:length(tableList)
    fprintf('Table: %s\n', tableList{idxTableList});
    optReadTable = detectImportOptions(tableList{idxTableList}, 'FileType','delimitedtext');
    tableComplete = readtable(tableList{idxTableList}, optReadTable);
    
    [~,fileName,~] = fileparts(tableList{idxTableList});
    
    tableComplete.index = categorical(tableComplete.index);
    indexLevels = categories(tableComplete.index);
    tableComplete.tempLevel = categorical(tableComplete.tempLevel);
    tempLevels = categories(tableComplete.tempLevel);
    fprintf('AnimalIndex\tTempLevel\tEvents\n');

    flagFirstEntry = 1;
    for idxIndexLevels = 1:length(indexLevels)
        indexLevel = indexLevels{idxIndexLevels};
        for idxTempLevels = 1:length(tempLevels)
            tempLevel = tempLevels{idxTempLevels};
            
            mask = (tableComplete.index == indexLevel) & (tableComplete.tempLevel == tempLevel);
            tableCondition = tableComplete(mask,:);
            
            fileNameTableEMod = [exportRootDir filesep fileName filesep tempLevel ' - ' 'M' indexLevel '.tsv'];
            optReadTable = detectImportOptions(fileNameTableEMod, 'FileType','delimitedtext');
            tableEMod = readtable(fileNameTableEMod, optReadTable);
            tableEMod(1,:) = [];

            fprintf('%s\t\t\t%s\t%d\n', indexLevel, tempLevel, height(tableCondition));
            
            eModulus = tableEMod.emodulus;
            viscosity = eModulus .* tableCondition.tau2; % (Pa*s = kPa * ms)
            tableEVisc = table(eModulus,viscosity);
            if flagFirstEntry
                tableExport = [tableCondition tableEVisc];
                flagFirstEntry = 0;
            else
                tableExport = [tableExport; [tableCondition tableEVisc]];
            end
            
            targetDir = [exportRootDir filesep fileName filesep tempLevel];
            
        end
    end
    writetable(tableExport, [fileName '_matProp' '.csv']);
end

return
%% Script

%% plot data
    recFig = 0;
    maskTau2 = (taus2Circularity >= 0) & (rSqrsCircularityTau2 >= 0.6) & (tau2Range(1) <= taus2Circularity) & (taus2Circularity <= tau2Range(2));
    maskArea = (areaGateRange(1) <= area) & (area <= areaGateRange(2));
    mask = maskTau2 & maskArea;
  % area histogram
    recFig = recFig + 1; setUpFigureHisto(recFig);
    plotHisto5( area(mask), area(mask), areaPlotRange, tsvFileName(1:idx-1), 'Area (µm^2)', 'norm', 5 );
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_areaHisto' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_areaHisto' '.png']);
  % eMod vs area
    recFig = recFig + 1; setUpFigure(recFig);
    plot(area(mask),eModulus(mask),'.');
    xlabel('Area (µm^2)'); ylabel('Young''s modulus (kPa)');
    title([tsvFileName(1:idx-1) sprintf(' (%d cells)', length(area(mask)))],'Interpreter','none');
    if flagPlotRanges
        xlim(areaPlotRange); ylim(eModPlotRange);
    end
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_eMod' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_eMod' '.png']);
  % eMod histogram
    recFig = recFig + 1; setUpFigureHisto(recFig);
    plotHisto5( eModulus(mask), eModulus(mask), eModPlotRange, tsvFileName(1:idx-1), 'Young''s modulus (kPa)', 'norm', 5 );
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_eModHisto' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_eModHisto' '.png']);
  % tau2 vs area
    recFig = recFig + 1; setUpFigure(recFig);
    plot(area(mask),taus2Circularity(mask),'.');
    xlabel('Area (µm^2)'); ylabel('\tau_2 (ms)');
    title([tsvFileName(1:idx-1) sprintf(' (%d cells)', length(area(mask)))],'Interpreter','none');
    if flagPlotRanges
        xlim(areaPlotRange); ylim(tau2PlotRange);
    end
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_tau2' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_tau2' '.png']);
  % tau2 histogram
    recFig = recFig + 1; setUpFigureHisto(recFig);
    plotHisto5( taus2Circularity(mask), taus2Circularity(mask), tau2PlotRange, tsvFileName(1:idx-1), '\tau_2 (ms)', 'norm', 5 );
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_tau2Histo' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_tau2Histo' '.png']);
  % visc vs area
    recFig = recFig + 1; setUpFigure(recFig);
    plot(area(mask),viscosity(mask),'.');
    xlabel('Area (µm^2)'); ylabel('viscosity (Pa s)');
    title([tsvFileName(1:idx-1) sprintf(' (%d cells)', length(area(mask)))],'Interpreter','none');
    if flagPlotRanges
        xlim(areaPlotRange); ylim(viscPlotRange);
    end
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_visc' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_visc' '.png']);
  % visc histogram
    recFig = recFig + 1; setUpFigureHisto(recFig);
    plotHisto5( viscosity(mask), viscosity(mask), viscPlotRange, tsvFileName(1:idx-1), 'viscosity (Pa s)', 'norm', 5 );
%     saveas(recFig,[dataDirPrefix measDate filesep exportFolder filesep tsvFileName(1:idx-1) '_viscHisto' '.fig']);
    print(recFig, '-dpng','-painters',[dataDirPrefix measDate filesep EModFolder filesep tsvFileName(1:idx-1) '_viscHisto' '.png']);
  